package com.westar.core.services;

import com.westar.core.conf.SysConst;
import com.westar.base.model.AffairInfo;
import com.westar.base.model.AffairMsg;
import com.westar.base.model.UserInfo;
import com.westar.base.pojo.WestarResult;
import com.westar.base.util.DateTimeUtil;
import com.westar.core.dao.AffairDao;
import com.westar.core.job.DynamicJobFactory;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;

@Service
public class AffairInfoService {

    @Autowired
    private AffairDao affairDao;

    @Resource
    DynamicJobFactory dynamicJobFactory;

    @Autowired
    private SystemLogService systemLogService;

    /**
     * 保存affairInfo 信息
     * @param sessionUser 当前操作人员
     * @param affairId 事项主键
     */
    public void addAffairInfo(UserInfo sessionUser, String affairId) {
        AffairInfo affairInfo = new AffairInfo();
        affairInfo.setAffairId(affairId);
        affairInfo.setOperator(sessionUser.getAccount()  );
        affairInfo.setState( "0" );
        //默认未有办结的
        affairInfo.setPerFinishState( "0" );
        affairDao.add(affairInfo);
    }

    /**
     * 修改数据状态
     * @param affairId 办件主键
     * @param state 修改后的状态
     */
    public void updateAffairInfo(String affairId,String state){
        String autoHandleTime = getAutoHandTime();
        affairDao.excuteSql( "update affairInfo set state="+state+" , autoHandleTime='"+autoHandleTime+"' where affairId='"+affairId+"'",null );
    }

    /**
     * 修改受理编号
     * @param affairId 办件主键
     * @param affairCode
     */
    public void updateAffairCodeAndHandTime(String affairId,String affairCode,String account){
        AffairInfo affairInfo = new AffairInfo();
        affairInfo.setAffairId(affairId);
        affairInfo.setAffairCode(affairCode);
        affairInfo.setOperator(account );
        String nowDateTime = DateTimeUtil.getNowDateStr( DateTimeUtil.yyyy_MM_dd_HH_mm );

        int minuts = (int)Math.random() * 2 + 1;
        String autoHandleTime = DateTimeUtil.addDate(nowDateTime,
                DateTimeUtil.yyyy_MM_dd_HH_mm , Calendar.MINUTE,minuts );

        affairInfo.setAutoHandleTime(autoHandleTime);

        affairDao.updateAffairCode(affairInfo);
    }

    /**
     * 根据受理编号查询办件信息
     * @param affairCode
     * @return
     */
    public AffairInfo getAffairInfoByCode(String affairCode){
        return affairDao.getAffairInfoByCode(affairCode);
    }

    /**
     * 修改办件状态
     * @param info 办件主键
     * @param perFinishState
     */
    public WestarResult updateAffairFinishState(AffairInfo info,String perFinishState){
        AffairInfo affairInfo = affairDao.getAffairInfoByCode(info.getAffairCode());
        if(null == affairInfo){
            systemLogService.addSystemLog("-",info.getAffairCode(),"-1","受理编号："+info.getAffairCode()+"在中间件不能查询！");
            return WestarResult.build( -1,"受理编号在中间件不能查询！" );
        }
        systemLogService.addSystemLog(affairInfo.getOperator(),affairInfo.getAffairCode(),"0","数据:"+info.getAffairCode()+"在政务系统已办结！");

        affairInfo.setPerFinishState(perFinishState);

        String nowDateTime = DateTimeUtil.getNowDateStr( DateTimeUtil.yyyy_MM_dd_HH_mm );
        int minuts = (int)Math.random() * 2 + 1;
        String autoHandleTime = DateTimeUtil.addDate(nowDateTime,
                DateTimeUtil.yyyy_MM_dd_HH_mm , Calendar.MINUTE,minuts );

        affairInfo.setAutoHandleTime(autoHandleTime);

        try {
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("affairInfo",affairInfo  );
            jobDataMap.put("type","editTask");

            String cron = DateTimeUtil.transCorn(autoHandleTime);
            dynamicJobFactory.addJob( AffairEditScheduledService.class,jobDataMap,affairInfo.getAffairId(),cron);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //放置制证办理信息
        affairInfo.setPermissionDecideDocName(info.getPermissionDecideDocName());
        affairInfo.setPermissionCategory(info.getPermissionCategory());
        affairInfo.setPermissionDocNo(info.getPermissionDocNo());
        affairInfo.setPermissionExplain(info.getPermissionExplain());
        affairInfo.setPermissionSdate(info.getPermissionSdate());
        affairInfo.setPermissionEdate(info.getPermissionEdate());
        affairInfo.setPermissionName(info.getPermissionName());
        affairInfo.setPermissionNo(info.getPermissionNo());

        affairDao.update(affairInfo);

        return WestarResult.ok( "成功上传" );
    }

    /**
     * 查询需要办理的信息
     * @return
     */
    public List<AffairInfo> listAffairInfoForAutoEdit(String state,String[] perFinishState) {
        String nowDateTime = DateTimeUtil.getNowDateStr( DateTimeUtil.yyyy_MM_dd_HH_mm );
        nowDateTime = DateTimeUtil.addDate(nowDateTime, DateTimeUtil.yyyy_MM_dd_HH_mm,Calendar.MINUTE, SysConst.SCHEDULAR_MINUS);
        List<AffairInfo> affairInfoList = affairDao.listAffairInfoForAutoEdit(nowDateTime,state,perFinishState);
        return affairInfoList;
    }

    /**
     * 删除办件信息
     * @param affairId
     */
    public void delAffairInfo(String affairId) {
        affairDao.delByField("affairInfo",new String[]{"affairId"}, new Object[]{affairId} );
    }

    public String getAutoHandTime(){
        String nowDateTime = DateTimeUtil.getNowDateStr( DateTimeUtil.yyyy_MM_dd_HH_mm );
        int minuts = (int)Math.random()*6 +  SysConst.SCHEDULAR_MINUS;
        String autoHandleTime = DateTimeUtil.addDate(nowDateTime,
                DateTimeUtil.yyyy_MM_dd_HH_mm , Calendar.MINUTE,minuts );
        return autoHandleTime;
    }

    /**
     * 查询不予受理的数据
     * @return
     */
    public List<AffairInfo> listAffairInfoForReject() {
        List<AffairInfo> affairInfoList = affairDao.listAffairInfoForReject();
        return affairInfoList;
    }

    /**
     * 发送消息用的
     * @param affairCode
     * @param status
     * @param msg
     */
    public void sendResult(String affairCode,Integer status,String msg){
        AffairInfo affairInfo = affairDao.getAffairInfoByCode(affairCode);
        String autoHandleTime = DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd_HH_mm_ss);
        if(null != affairInfo){
            //自动办理时间
            autoHandleTime = affairInfo.getAutoHandleTime();
        }

        AffairMsg affairMsg = affairDao.getAffairMsgByCode(affairCode);
        if(null == affairMsg){
            affairMsg = new AffairMsg();
            affairMsg.setAffairCode(affairCode);
            affairMsg.setState(status);
            affairMsg.setAutoHandleTime(autoHandleTime);

            affairMsg.setMsg(msg);
            affairMsg.setTimes(1);
            affairDao.add(affairMsg);

        }else{
            affairMsg.setState(status);
            affairMsg.setAutoHandleTime(autoHandleTime);
            affairMsg.setMsg(msg);
            affairMsg.setTimes(1);

            affairDao.update(affairMsg);
        }
        //TODO 开始网络请求调用

    }
}
